package ru.amse.baltijsky.javascheme.nodeshape;

import ru.amse.baltijsky.javascheme.util.Dir;
import ru.amse.baltijsky.javascheme.util.Size;

import java.awt.*;

/**
 * A convenience implementation skeleton for node shape.
 */
public abstract class AbstractNodeShape implements INodeShape {
    /**
     * The code that corresponds to the schema tree node for which the shape is created.
     */
    private String code;

    /**
     * Internal constructor, used to encapsulate the work with the code string and calculate node dimensions.
     *
     * @param code the code of the node
     */
    protected AbstractNodeShape(String code) {
        this.code = code;
    }

    /**
     * Returns the size of the rectangle large enough to contain the code it it were drawn in the current settings
     * of the specified graphics context.
     *
     * @param g graphics context
     * @return size of the smallest rectangle containing the code
     */
    protected Size getCodeSize(Graphics g) {
        return new Size(g.getFontMetrics().stringWidth(code), g.getFontMetrics().getHeight());
    }

    /**
     * Internal getter for the node code.
     *
     * @return node code
     */
    protected String getCode() {
        return code;
    }

    /**
     * Internal setter for the node code. Used to wrap the code.
     *
     * @param theCode the code to set
     */
    protected void setCode(String theCode) {
        code = theCode;
    }

    public int[] getShapeDims(Graphics g) {
        Size rect = getBoundRectSize(g);
        int[] ret = new int[Dir.length()];
        ret[Dir.l()] = rect.x / 2;
        ret[Dir.r()] = rect.x - ret[Dir.l()];
        ret[Dir.u()] = rect.y / 2;
        ret[Dir.d()] = rect.y - ret[Dir.u()];
        return ret;
    }
}
